﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using Yunwei.Domain.Models;

namespace Yunwei.Web.Areas.Admin.Models
{
    public class VendorViewModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(20, MinimumLength = 2, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "供应商名称")]
        public string Name { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(20, MinimumLength = 2, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "联系人")]
        public string Contact { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(20, MinimumLength = 7, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "手机")]
        public string Phone { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(200, MinimumLength = 4, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "地址")]
        public string Address { get; set; }

        [Required(ErrorMessage = "必填")]
        [Display(Name = "经度")]
        public decimal X { get; set; }

        [Required(ErrorMessage = "必填")]
        [Display(Name = "纬度")]
        public decimal Y { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(200, MinimumLength = 5, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "照片")]
        public string Avatar { get; set; }

        [Required(ErrorMessage = "必填")]
        public int User_Id { get; set; }

        [Display(Name = "详述")]
        public string Description { get; set; }

        [NotMapped]
        public Vendor Vendor
        {
            get
            {
                Vendor vendor = new Vendor()
                {
                    Id = this.Id,
                    Name = this.Name,
                    Address = this.Address,
                    Phone = this.Phone,
                    X = this.X,
                    Y = this.Y,
                    Avatar = this.Avatar,
                    User_Id = this.User_Id,
                    Description = this.Description,
                    Contact = this.Contact
                };
                
                return vendor;
            }
        }

        public static VendorViewModel FromVendor(Vendor vendor)
        {
            VendorViewModel vi = new VendorViewModel();
            if (vendor == null) return vi;

            vi.Id = vendor.Id;
            vi.Name = vendor.Name;
            vi.Address = vendor.Address;
            vi.Phone = vendor.Phone;
            vi.X = vendor.X;
            vi.Y = vendor.Y;
            vi.Avatar = vendor.Avatar;
            vi.User_Id = vendor.User_Id;
            vi.Description = vendor.Description;
            vi.Contact = vendor.Contact;
            return vi;
        }
    }
}